package com.eve.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import com.eve.DAObjects.ClientDAO;
import com.eve.crp.Client;
import com.eve.crp.ClientValidator;
import com.eve.crp.GenerateOptionList;

@Controller
@RequestMapping("ApplInfo.html")
public class ClientController extends BaseController{
	ClientValidator clientValidator;
	GenerateOptionList optionList;
	
	@Autowired
	public ClientController(ClientValidator clientValidator) throws IOException, ClassNotFoundException, SQLException{
		this.clientValidator = clientValidator;
	}
	
   @RequestMapping(value="/ApplInfo.html", method = RequestMethod.GET)
   public String initForm(ModelMap model) throws SQLException{
	   Client client = new Client();
	   client.setZip(26505);
	   model.addAttribute("client", client);
	   return "/internal/ApplInfo";
   }
   @RequestMapping(value="/ApplInfo.html", method = RequestMethod.POST)
   public String processSubmit(@ModelAttribute("client") Client client, BindingResult result, SessionStatus status) throws FileNotFoundException, ClassNotFoundException, SQLException, IOException{
	   clientValidator.validate(client, result);
	   if(result.hasErrors()){
		   return "internal/ApplInfo";
	   } else {
		   clientDao.createCase(client);
		   status.setComplete();
		   return "internal/ApplInfo";
	   }
   }
}